import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import stringify from 'Shared/utils/stringify';
import {formatMessage} from './intl';
const baseUrl = 'afterSales/api/v1';
import * as dto from './DTO';

const getListDesc = formatMessage({
    id: 'api.message.getListDesc',
    defaultMessage: '查询特殊零件替换关系列表'
});
const getDetailDesc = formatMessage({
    id: 'api.message.getDetailDesc',
    defaultMessage: '查询特殊零件替换关系详情'
});
const addDesc = formatMessage({
    id: 'api.message.addDesc',
    defaultMessage: '新增特殊零件替换关系'
});
const updateDesc = formatMessage({
    id: 'api.message.updateDesc',
    defaultMessage: '编辑特殊零件替换关系'
});
const abandonDesc = formatMessage({
    id: 'api.message.abandonDesc',
    defaultMessage: '作废特殊零件替换关系'
});
const importTempDesc = formatMessage({
    id: 'api.message.importTempDesc',
    defaultMessage: '导入模板'
});
const getDutyUnitsDesc = formatMessage({
    id: 'api.message.getDutyUnitsDesc',
    defaultMessage: '供应商查询'
});
const partQueryDesc = formatMessage({
    id: 'api.message.partQueryDesc',
    defaultMessage: '备件查询'
});
const productCategoryQueryDesc = formatMessage({
    id: 'api.message.productCategoryQueryDesc',
    defaultMessage: '车型查询'
});

//初始化
export const getInitData = () => request(`${baseUrl}/brands`, null, dto.InitDataStruct)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getListDesc));

// 查询
export const getList = bodyCondition => {
    const queryString = stringify(bodyCondition);
    return request(`${baseUrl}/specialPartReplaces?${queryString}`, null, dto.List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};
// 查询详情
export const getDetail = id => request(`${baseUrl}/specialPartReplaces/${id}`, null, null)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getDetailDesc));

// 新增
export const addData = data => request(`${baseUrl}/specialPartReplaces`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, addDesc));

// 编辑
export const updateData = (id, data) => request(`${baseUrl}/specialPartReplaces/${id}`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, updateDesc));
// 作废
export const abandonData = (id, data) => request(`${baseUrl}/specialPartReplaces/${id}/abandon`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, abandonDesc));

export const importTemp = () => request(`${baseUrl}/specialPartReplaces/importTemp`)
    .then(handleFetchResultNotification(REQUEST_TYPE.submit, importTempDesc)).then(res => res.data);

// 快捷查询责任单位
export const getDutyUnitsQuickly = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dutyUnits/base/simple?${queryString}`, null, dto.SelectList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDutyUnitsDesc));
};
// 查询责任单位
export const getDutyUnits = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dutyUnits/base?${queryString}`, null, dto.SelectList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDutyUnitsDesc));
};
//主机厂备件查询
export const partQuery = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/parts/forTechshipment?${queryString}`, null, dto.SelectList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, partQueryDesc));
};
//车型查询
export const productCategoryQuery = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/productCategories?${queryString}`, null, dto.SelectList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, productCategoryQueryDesc));
};
